let reqLogin = null; // 登录轮询定时器
if (isWeiXin()) { // 微信端
    if (!refer.includes("http://b.s11.cn")) {
        localStorage.setItem("refer", refer)
    }
    let wx_token = getQueryVariable("m_token");
    wx_token && localStorage.setItem("m_token", wx_token);

    let fromType = getQueryVariable("fromType");
    fromType && localStorage.setItem("fromType", fromType);
    let urls = localStorage.getItem("refer");
    $(".url").val(urls)
    let token = localStorage.getItem("m_token");

    let actions = getQueryVariable("ac"); // 判断是否从公众号跳转
    actions && localStorage.setItem("actions", actions)
    if (token) {
        $(".login-mask").hide();
        let token = localStorage.getItem("m_token");
        $.ajax({
            type: "GET",
            url: `//u.bendibao.com/?controller=fankui&token=${token}&action=checkSubscribe`,
            dataType: "json",
            success: function (res) {
                if (res.code === 0) {
                    localStorage.setItem("sub", res.subscribe)
                }
            }
        });
        let action = localStorage.getItem("actions")
        if (action == "record") {
            $(".tab").eq(1).addClass("tab-select").siblings().removeClass("tab-select");
            $(".tab").eq(1).find("img").eq(0).show().siblings().hide();
            $(".tab").eq(1).siblings().find("img").eq(0).hide().siblings().show();
            let content = $(".tab").eq(1).children("div").eq(1).text()
            if (content == "我要反馈") {
                $(".feed").show();
                $(".record").hide();
            } else {
                $(".feed").hide();
                $(".record").show();
                getRecord();
            }
            localStorage.removeItem("actions")
        }
    } else {
        $(".login-mask").show();
    }



} else { // 非微信端
    let urls = document.referrer;
    $(".url").val(urls)
    let token = localStorage.getItem("m_token");
    if (!token) {
        $.ajax({
            type: "GET",
            url: `//u.bendibao.com/?controller=fankui&action=createCustomerInfo`,
            dataType: "json",
            success: function (res) {
                if (res.code === 0) {
                    localStorage.setItem("m_token", res.data.token);
                }
            }
        });
    }
}
/**
 * 底部菜单导航
 */
$(".tab").click(function () {
    $(this).addClass("tab-select").siblings().removeClass("tab-select");
    $(this).find("img").eq(0).show().siblings().hide();
    $(this).siblings().find("img").eq(0).hide().siblings().show();
    let content = $(this).children("div").eq(1).text()
    if (content == "我要反馈") {
        $(".feed").show();
        $(".record").hide();
    } else {
        $(".feed").hide();
        $(".record").show();
        getRecord();
    }
})

// 获取历史记录
function getRecord() {
    let path = localStorage.getItem("head_img");

    if (path && path !== null) {
        $(".user").children("img").attr("src", path);
    }

    if (isWeiXin()) {
        // 获取历史反馈
        let token = localStorage.getItem("m_token");
        $.ajax({
            type: "GET",
            url: `//u.bendibao.com/?controller=fankui&action=getHistory&token=${token}`,
            dataType: "json",
            success: function (res) {
                console.log(res)
                if (res.code === 0) {
                    let record = res.data;
                    let count = record.length;
                    $(".feed-count").text(count);
                    $(".record-ul").html("");
                    record.forEach(el => {
                        if (el.examine_content == "") {
                            $(".record-ul").append(`<div class="record-list">
                        <div class="desc-content">`+ el.feed_content + `</div>
                        <div class="desc-time">`+ el.add_time + `</div>
                        </div>`)
                        }else{
                            $(".record-ul").append(`<div class="record-list">
                        <div class="desc-content">`+ el.feed_content + `</div>
                        <div class="desc-time">`+ el.add_time + `</div>
                        <div class="desc-reply"> <span>反馈回复:</span>`+ el.examine_content + `</div>
                        </div>`)
                        }
                        
                    })
                } else if (res.code === -2) {
                    $(".login-mask").show()
                }
            }
        });
    } else {
        // 获取历史反馈
        let token = localStorage.getItem("m_token");
        $.ajax({
            type: "GET",
            url: `//u.bendibao.com/?controller=fankui&action=getHistory&token=${token}&m=1`,
            dataType: "json",
            success: function (res) {
                if (res.code === 0) {
                    let record = res.data;
                    let count = record.length;
                    $(".feed-count").text(count);
                    $(".record-ul").html("");
                    record.forEach(el => {
                        if (el.examine_content == "") {
                            $(".record-ul").append(`<div class="record-list">
                        <div class="desc-content">`+ el.feed_content + `</div>
                        <div class="desc-time">`+ el.add_time + `</div>
                        </div>`)
                        }else{
                            $(".record-ul").append(`<div class="record-list">
                        <div class="desc-content">`+ el.feed_content + `</div>
                        <div class="desc-time">`+ el.add_time + `</div>
                        <div class="desc-reply"> <span>反馈回复:</span>`+ el.examine_content + `</div>
                        </div>`)
                        }
                    })
                }
            }
        });
    }

}

/**
 * 获取文本框的字数长度
 */
$(".desc").keyup(function () {
    let length = $(this).val().length
    $(this).siblings(".inp-length").text(`${length}/200`)
});

/**
 * 发送按钮事件
 */
$(".send").click(function () {
    let url = $(".url").val();
    let feedContent = $(".desc").val();
    let tel = $(".tel").val();
    let feedLth = feedContent.length
    if (feedContent == "" || tel == "") {
        $(".tips-dia").children(".tips-content").text("请补充反馈内容或联系方式后再提交")
        $(".mask").css("display", "flex");
        return
    } else if (feedLth < 10) {
        $(".tips-dia").children(".tips-content").text("反馈描述不得少于10个字")
        $(".mask").css("display", "flex");
        return
    }


    // 发送成功后
    let cityName = localStorage.getItem("cityname") || "";
    let token = localStorage.getItem("m_token");
    if (isWeiXin()) { // 微信端请求发送接口
        let sub = localStorage.getItem("sub")
        $.ajax({
            type: "POST",
            url: `//u.bendibao.com/?controller=fankui&action=saveFeedContent`,
            data: {
                cityname: cityName,
                url: url,
                feed_content: feedContent,
                token: token,
                tel: tel
            },
            dataType: "json",
            success: function (res) {
                if (res.code === 0) {
                    if (sub == 1) {
                        $(".wx-gzh").hide();
                        $(".send-desc").text("感谢您的反馈");
                        $(".send-title").text("成功提交");
                        $(".send-footer").hide();
                        $(".send-mask").css("display", "flex");
                        $(".send-success").show();
                        setTimeout(() => {
                            $(".send-mask").hide();
                            $(".send-success").hide();
                        }, 1500)
                    } else {
                        $(".send-mask").css("display", "flex");
                        $(".qr-code").hide();
                        $(".wx-gzh").hide();
                        $(".send-title").text("成功提交");
                        $(".send-desc").text("马上开通提醒，有新回复第一时间通知你");
                        $(".send-close").text("取消")
                        $(".open-qr-code").show();
                        $(".send-success").show();
                    }
                    $(".tel").val("");
                    $(".desc").val("");
                    $(".url").val("");
                    $(".inp-length").text("0/200");
                } else if (res.code === -1) {
                    $(".tips-dia").children(".tips-content").text(res.msg)
                    $(".mask").css("display", "flex");
                    $(".tel").val("");
                    $(".desc").val("");
                    $(".url").val("");
                    $(".inp-length").text("0/200");
                } else if (res.code === -2) {
                    $(".login-mask").show()
                }


            }
        });
    } else { // 非微信端请求接口
        $.ajax({
            type: "POST",
            url: `//u.bendibao.com/?controller=fankui&action=saveFeedContent&m=1`,
            data: {
                cityname: cityName,
                url: url,
                feed_content: feedContent,
                token: token,
                tel: tel
            },
            dataType: "json",
            success: function (res) {
                if (res.code === 0) {
                    $(".send-desc").text("感谢您的反馈");
                    $(".send-title").text("成功提交");
                    $(".send-footer").hide();
                    $(".send-mask").css("display", "flex");
                    $(".send-success").show();
                    setTimeout(() => {
                        $(".send-mask").hide();
                        $(".send-success").hide();
                    }, 1500)
                    $(".tel").val("");
                    $(".desc").val("");
                    $(".url").val("");
                    $(".inp-length").text("0/200");
                } else if (res.code === -1) {
                    $(".tips-dia").children(".tips-content").text(res.msg)
                    $(".mask").css("display", "flex");
                    $(".tel").val("");
                    $(".desc").val("");
                    $(".url").val("");
                    $(".inp-length").text("0/200");
                }
            },
        });
    }






})

/**
 * 扫码关注事件
 */
// function getFollow() {
//     let token = localStorage.getItem("m_token");
//     let timec = new Date().getTime();
//     $.ajax({
//         type: "GET",
//         url: `//u.bendibao.com/?controller=fankui&action=checkLogin&m=1&token=${token}&t=${timec}`,
//         dataType: "json",
//         success: function (res) {
//             if (res.code === 0) {
//                 let nowTime = new Date().getTime();
//                 localStorage.setItem("startTime", nowTime);
//                 clearInterval(reqLogin);
//                 $(".qr-code").hide();
//                 $(".send-title").text("成功关注");
//                 $(".send-desc").text("我们将在三个工作日内处理您的反馈");
//                 $(".send-close").text("好的")
//                 $(".open-qr-code").hide();
//                 $(".send-success").show();
//                 $(".qr-img").attr("src", "");
//                 localStorage.setItem("m", "1");

//             }
//         }
//     });
// }

/**
 * 关闭提醒弹出框
 */

$(".tips-btn").click(function () {
    $(".mask").hide();
})

/**
 * 关闭成功发送弹窗
 */

$(".send-close").click(function () {
    clearInterval(reqLogin);
    $(".send-mask").hide();
    let record = localStorage.getItem("record")
    if (record == "1") {
        getRecord()
        localStorage.removeItem("record")
    }
})

/**
 * 打开微信公众号二维码
 */

$(".open-qr-code").click(function () {
    if (isWeiXin()) {
        // -------------微信浏览器端
        $(".send-success").hide();
        $(".wx-gzh").css("display", "flex");
    }
})

/**
* 关闭微信公众号二维码
*/
$(".qr-close").click(function () {
    $(".send-mask").hide();
    clearInterval(reqLogin);
    if (isWeiXin()) {
        let token = localStorage.getItem("m_token");
        if (token) {
            let token = localStorage.getItem("m_token");
            $.ajax({
                type: "GET",
                url: `//u.bendibao.com/?controller=fankui&token=${token}&action=checkSubscribe`,
                dataType: "json",
                success: function (res) {
                    if (res.code === 0) {
                        localStorage.setItem("sub", res.subscribe)
                    }
                }
            });
        }
    }
})

$(".follow-btn").click(function () {
    location.href = "http://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzA4NzU2OTE2MQ==&scene=110#wechat_redirect"
})

$(".go-back").click(function () {
    window.history.go(-1)
})


$(".login-btn").click(function () {
    window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxfd57cfd4907a66cb&redirect_uri=http://b.s11.cn/fankui_login.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
})

/**
  * 关闭登录框
  */
$(".login-close").click(function () {
    window.history.go(-1)
})

// 登录失败重新获取token
// function reGetToken() {
//     $.ajax({
//         type: "GET",
//         url: `//u.bendibao.com/?controller=fankui&action=createCustomerInfo`,
//         dataType: "json",
//         success: function (res) {
//             if (res.code === 0) {
//                 localStorage.setItem("m_token", res.data.token);
//                 // -------------非微信浏览器端
//                 let token = localStorage.getItem("m_token");
//                 $(".qr-img").hide();
//                 $(".loading").show();
//                 $(".qr-img").attr('src', "");
//                 $(".send-success").hide();
//                 $(".qr-code").css("display", "flex");
//                 let timec = new Date().getTime();
//                 // 获取二维码请求
//                 $.ajax({
//                     type: "GET",
//                     url: "//u.bendibao.com/",
//                     dataType: "json",
//                     data: {
//                         controller: "fankui",
//                         action: "getQrCode",
//                         token,
//                         t: timec
//                     },
//                     success: function (res) {
//                         let path = res.data.qrCode; // 获取二维码路径
//                         $(".qr-img").attr('src', path);
//                         $(".loading").hide();
//                         $(".qr-img").show();
//                         reqLogin = setInterval(getFollow, 2000);
//                     }
//                 });
//             }
//         }
//     });

// }